<?php

Class Backend {

    public static function getMainMenu() {
        $allMenu = array(
            //Home
            'Home' => array('label' => Yii::t('BackEnd', 'Home'), 'url' => array('/site/index')),
            //Content
            'CMS' => array('label' => Yii::t('BackEnd', 'Content'), 'iconUrl' => Yii::app()->request->baseUrl . '/css/admin/images/icon-menu/content.png',
                'subs' => array(
                    'Article' => array(
                        'label' => Yii::t('BackEnd', 'Article'), 'url' => array('/articles/admin'), 'visible' => UserAccess::checkAccess('articlesAdmin'),
                        'subs' => array(
                            '/aCategory/admin' => array('label' => Yii::t('BackEnd', 'Article categories'), 'url' => array('/aCategory/admin'), 'visible' => UserAccess::checkAccess('aCategoryAdmin')),
                        )),
                    'Product' => array(
                        'label' => Yii::t('BackEnd', 'Product'), 'url' => array('/product/admin'), 'visible' => UserAccess::checkAccess('articlesAdmin'),
                        'subs' => array(
                            '/maker/admin' => array('label' => Yii::t('BackEnd', 'Manage maker'), 'url' => array('/maker/admin'), 'visible' => UserAccess::checkAccess('makerAdmin')),
                            '/partsLists/admin' => array('label' => Yii::t('BackEnd', 'Manage partsLists'), 'url' => array('/partsLists/admin'), 'visible' => UserAccess::checkAccess('partsListsAdmin')),
                            '/accessories/admin' => array('label' => Yii::t('BackEnd', 'Accessories'), 'url' => array('/accessories/admin'), 'visible' => UserAccess::checkAccess('accessoriesAdmin')),
                            //product
                            '/productCategory/admin' => array('label' => Yii::t('BackEnd', 'Product category'), 'url' => array('/productCategory/admin'), 'visible' => UserAccess::checkAccess('productCategoryAdmin')),
                            '/product/admin' => array('label' => Yii::t('BackEnd', 'Product'), 'url' => array('/product/admin'), 'visible' => UserAccess::checkAccess('productAdmin')),
                        )),
                ),
                'controllers' => array('Article','Product','maker','partsLists','productCategory','accessories','aCategory','product')
            ),
            'Setting' => array('label' => Yii::t('BackEnd', 'System'), 'iconUrl' => Yii::app()->request->baseUrl . '/css/admin/images/icon-menu/system.png',
                'subs' => array(
                    '/emailTemplate/admin' => array('iconUrl' => Yii::app()->request->baseUrl . '/css/admin/images/icon-menu/emailTemplate.png', 'label' => Yii::t('BackEnd', 'Email template'), 'url' => array('/emailTemplate/admin'), 'visible' => UserAccess::checkAccess('emailTemplateAdmin')),
                    '/adminUser/index' => array('iconUrl' => Yii::app()->request->baseUrl . '/css/admin/images/icon-menu/account.png', 'label' => Yii::t('BackEnd', 'Account manager'), 'url' => array('/adminUser/index'), 'visible' => UserAccess::checkAccess('adminUserIndex')),
                    '/support/admin' => array('label' => Yii::t('BackEnd', 'Support'), 'url' => array('/support/admin'), 'visible' => UserAccess::checkAccess('supportAdmin')),
                    '/menuItem/admin' => array('label' => Yii::t('BackEnd', 'Menu'), 'url' => array('/menuItem/admin'), 'visible' => UserAccess::checkAccess('menuItemAdmin')),
                    '/seo/admin' => array('label' => Yii::t('BackEnd', 'Seo'), 'url' => array('/seo/admin'), 'visible' => UserAccess::checkAccess('seoAdmin')),
                    '/srbac' => array('iconUrl' => Yii::app()->request->baseUrl . '/css/admin/images/icon-menu/authitem.png', 'label' => Yii::t('BackEnd', 'Grant Permission'), 'url' => array('/srbac'), 'visible' => UserAccess::checkAccess('srbac')),
                    'configuration' => array(
                        'iconUrl' => Yii::app()->request->baseUrl . '/css/admin/images/icons/11_12x12.png',
                        'label' => Yii::t('BackEnd', 'Setting manager'), 'url' => array('/config/setting'), 'visible' => UserAccess::checkAccess('configSetting'),
                        'subs' => array(
                            '/config/setting' => array('iconUrl' => Yii::app()->request->baseUrl . '/css/admin/images/icons/11_12x12.png', 'label' => Yii::t('BackEnd', 'Setting'), 'url' => array('/config/setting'), 'visible' => UserAccess::checkAccess('configSetting')),
                            '/config/index' => array('iconUrl' => Yii::app()->request->baseUrl . '/css/admin/images/icons/3_12x12.png', 'label' => Yii::t('BackEnd', 'Config manager'), 'url' => array('/config/admin'), 'visible' => UserAccess::checkAccess('configAdmin')),
                            '/configGroup/index' => array('iconUrl' => Yii::app()->request->baseUrl . '/css/admin/images/icons/14_12x12.png', 'label' => Yii::t('BackEnd', 'Config group manager'), 'url' => array('/configGroup/admin'), 'visible' => UserAccess::checkAccess('configGroupAdmin')),
                        )),
                ),
                'controllers' => array('emailTemplate', 'adminUser', 'config', 'srbac','menuItem','support','seo')
            ),
            'FrontEnd' => array('iconUrl' => Yii::app()->request->baseUrl . '/css/admin/images/icon-menu/home.png', 'label' => Yii::t('BackEnd', 'Frontend'), 'url' => (isset(Yii::app()->params->systemFrontEndUrl)) ? Yii::app()->params->systemFrontEndUrl : '#', 'target' => '_blank'),
        );

        self::rebuildArray($allMenu);
        return $allMenu;
    }

    public static function rebuildArray(&$allMenu) {
        foreach ($allMenu as $k => $menu) {
            if (isset($menu['subs']) && is_array($menu['subs'])) {
                foreach ($menu['subs'] as $ksub => $item) {
                    if (isset($item['visible']) && !$item['visible']) {
                        unset($menu['subs'][$ksub]);
                    }
                }
            }

            //Assign url in subs item to Parent
            if (isset($menu['url'])) {
                $allMenu[$k]['url'] = $menu['url'];
            } elseif (sizeof($menu['subs'])) {
                $item = array_shift(array_values($menu['subs']));
                $allMenu[$k]['url'] = $item['url'][0];
            }
            else
                unset($allMenu[$k]);
        }
    }

    public static function getModuleSlideBar($module, $activeItem, $level2 = '') {
        $mainMenu = self::getMainMenu();
        if ($level2)
            $slideBar = $mainMenu[$module]['subs'][$level2]['subs'];
        else
            $slideBar = $mainMenu[$module]['subs'];
        if (!isset($slideBar[$activeItem]))
            return array();
        $slideBar[$activeItem]['url'] = "#";
        $slideBar[$activeItem]['class'] = 'active';
        return $slideBar;
    }

}
?>

